package cn.stylefeng.guns.modular.time.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.Date;

import cn.stylefeng.guns.modular.time.entity.FileTransfers;
import cn.stylefeng.guns.modular.time.mapper.FileTransfersMapper;
import cn.stylefeng.guns.modular.time.model.request.FileTransfersRequest;
import cn.stylefeng.guns.modular.time.service.FileTransfersService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;

/**
 * 文件传输任务业务实现层
 */
@Service
public class FileTransfersServiceImpl extends ServiceImpl<FileTransfersMapper, FileTransfers> implements FileTransfersService {

    /**
     * 分页查询文件传输任务
     * @param request 查询参数
     * @return 分页结果
     */
    @Override
    public PageResult<FileTransfers> findPage(FileTransfersRequest request) {
        LambdaQueryWrapper<FileTransfers> qw = new LambdaQueryWrapper<>();
        qw.eq(FileTransfers::getDelFlag, "N");

        // 搜索条件（文件路径、code、内网IP）
        if (request.getSearchText() != null && !request.getSearchText().trim().isEmpty()) {
            String searchText = request.getSearchText().trim();
            qw.and(w -> w.like(FileTransfers::getFilePath, searchText)
                    .or().like(FileTransfers::getCode, searchText)
                    .or().like(FileTransfers::getIntranetIp, searchText));
        }

        // 排序（默认创建时间倒序）
        if ("code".equals(request.getSortField())) {
            qw.orderBy(true, "asc".equals(request.getSortOrder()), FileTransfers::getCode);
        } else if ("filePath".equals(request.getSortField())) {
            qw.orderBy(true, "asc".equals(request.getSortOrder()), FileTransfers::getFilePath);
        } else if ("intranetIp".equals(request.getSortField())) {
            qw.orderBy(true, "asc".equals(request.getSortOrder()), FileTransfers::getIntranetIp);
        } else {
            qw.orderByDesc(FileTransfers::getCreateTime);
        }

        Page<FileTransfers> page = this.page(new Page<>(request.getPageNo(), request.getPageSize()), qw);
        return PageResultFactory.createPageResult(page);
    }

    /**
     * 新增文件传输任务
     * @param request 新增参数
     */
    @Override
    public void add(FileTransfersRequest request) {
        FileTransfers ft = new FileTransfers();
        ft.setFilePath(request.getFilePath());
        ft.setCode(request.getCode());
        ft.setIntranetIp(request.getIntranetIp());
        ft.setDelFlag("N");
        ft.setCreateTime(new Date());
        ft.setCreateUser(LoginContext.me().getLoginUser().getUserId());
        this.save(ft);
    }

    /**
     * 编辑文件传输任务
     * @param request 编辑参数
     */
    @Override
    public void edit(FileTransfersRequest request) {
        FileTransfers ft = this.getById(request.getTransferId());
        if (ft != null) {
            ft.setFilePath(request.getFilePath());
            ft.setCode(request.getCode());
            ft.setIntranetIp(request.getIntranetIp());
            ft.setUpdateTime(new Date());
            ft.setUpdateUser(LoginContext.me().getLoginUser().getUserId());
            this.updateById(ft);
        }
    }

    /**
     * 删除文件传输任务
     * @param request 删除参数
     */
    @Override
    public void delete(FileTransfersRequest request) {
        this.removeById(request.getTransferId());
    }

    /**
     * 查看任务详情
     * @param request 详情参数
     * @return 文件传输任务详情
     */
    @Override
    public FileTransfers detail(FileTransfersRequest request) {
        return this.getById(request.getTransferId());
    }
}